package com.zhejiangzhengyuan.municipal_flooded_road.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.WarnEvent;
import com.zhejiangzhengyuan.municipal_common.entity.dto.NewCase;
import com.zhejiangzhengyuan.municipal_common.entity.dto.ReceiptCaseDto;
import com.zhejiangzhengyuan.municipal_common.entity.dto.SendCase;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseIsHandledVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseNoHandle;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseTrackVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.CaseManagementService;
import com.zhejiangzhengyuan.municipal_common.service.CaseService;
import com.zhejiangzhengyuan.municipal_common.service.WarnEventService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** @author lin_ju Create on 2019/12/11 */
@Api(tags = "案件管理")
@RequestMapping("/ponding/case")
@RestController
@Slf4j
public class CaseManageRoadGatherWaterController {
  private final String caseType = "道路积水案件";
  private final String warnType = ConstantUtil.WARN_TYPE.FLOODED_ROAD;
  private final String module = "municipal_flooded_road";

  @Autowired private CaseManagementService caseManagementService;

  @Autowired private CaseService caseService;

  @Autowired private WarnEventService warnEventService;

  @ApiOperation("查询案件在未派单时的数据 分页查询")
  @GetMapping("/selectPageAll")
  public ResponseVo selectPageAll(
      @ApiParam("页码") Long pageNo,
      @ApiParam("每页显示条数") Long pageSize,
      String dataOrigin,
      String deviceCode,
      String reason,
      String username,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为15
    pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
    pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
    Page<CaseNoHandle> page = new Page<>(pageNo, pageSize);
    Page<CaseNoHandle> all =
        caseManagementService.selectAll(
            page, warnType, dataOrigin, deviceCode, reason, username, startTimestamp, endTimestamp);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation("导出道路积水案件导出未处理案件")
  @RequestMapping(value = "/exportNoHandleToExcel", method = RequestMethod.GET)
  public void exportNoHandleToExcel(HttpServletResponse response) {
    caseManagementService.exportNoHandleToExcel(response, warnType);
  }

  @ApiOperation("导出道路积水案件导出正在处理案件")
  @RequestMapping(value = "/exportHandlingToExcel", method = RequestMethod.GET)
  public void exportHandlingToExcel(HttpServletResponse response) {
    caseManagementService.exportHandlingToExcel(response, caseType, warnType);
  }

  @ApiOperation("导出道路积水案件导出已处理案件")
  @RequestMapping(value = "/exportIsHandledToExcel", method = RequestMethod.GET)
  public void exportIsHandledToExcel(HttpServletResponse response) {
    caseManagementService.exportIsHandledToExcel(response, caseType, warnType);
  }

  @ApiOperation("道路积水案件复位")
  @PostMapping("/updateWarnEvent")
  public ResponseVo updateWarnEvent(@RequestBody WarnEvent warnEvent) {
    if (warnEvent.getUpdateTime() == null) {
      return new ResponseVo(ConstantUtil.ResultCode.CLIENT_PARAM_ERROR);
    }
    return warnEventService.updateWarnEvent(warnEvent) > 0
        ? ResponseVo.SUCCESS
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("案件派单和重新派单接口")
  @PostMapping("/sendCase")
  public ResponseVo SendCase(@ModelAttribute SendCase sendCase) throws IOException {

    return caseService.saveCase(sendCase, caseType, module);
  }

  @ApiOperation("新建案件")
  @PostMapping("/newCase")
  public ResponseVo NewCase(@Validated @ModelAttribute NewCase newCase) throws IOException {
    return caseManagementService.insertCaseAndWarn(newCase, module, caseType, warnType);
  }

  @ApiOperation("道路积水案件回执")
  @PostMapping("/receiptCase")
  public ResponseVo receiptCase(@Validated @ModelAttribute ReceiptCaseDto receiptCaseDto) {
    long i = caseService.receiptCase(receiptCaseDto, module);
    return ResponseVo.successWithData(i);
  }

  @ApiOperation("道路积水案件回执打回")
  @GetMapping("/repeatReceipt")
  public ResponseVo receiptCase(Long cid) {
    int i = caseService.repeatReceipt(cid);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("派单新建多文件上传")
  @PostMapping("/uploadToFile")
  public ResponseVo uploadToFile(long cid, MultipartFile file) throws IOException {
    if (file == null) {
      return ResponseVo.CLIENT_PARAM_ERROR;
    }
    int i = caseService.uploadToFile(cid, file, module);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("回执多文件上传")
  @PostMapping("/uploadReceiptToFile")
  public ResponseVo uploadReceiptToFile(long rid, MultipartFile file) throws IOException {
    int i = caseService.uploadReceiptToFile(rid, file, module);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("道路积水案件从处理中转向已处理")
  @GetMapping("/caseHandingTurnToCaseIsHandled")
  public ResponseVo caseHandingTurnToCaseIsHandled(Long cid) {
    int i = caseService.caseHandingTurnToCaseIsHandled(cid);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("道路积水案件在处理中的数据 分页查询")
  @GetMapping("/selectCaseHandlingPage")
  public ResponseVo selectCaseHandlingPage(
      Long pageNo,
      Long pageSize,
      String dataOrigin,
      String deviceCode,
      String reason,
      String username,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为14
    if (pageNo == null || pageNo <= 0L) {
      pageNo = 1L;
    }
    pageSize = Objects.isNull(pageSize) || pageSize <= 0L ? 14 : pageNo;
    Page<CaseNoHandle> page = new Page<>(pageNo, pageSize);
    Page<CaseNoHandle> handlePage =
        caseManagementService.selectCaseHandlingPage(
            page,
            caseType,
            warnType,
            dataOrigin,
            deviceCode,
            reason,
            username,
            startTimestamp,
            endTimestamp);
    return ResponseVo.successWithData(handlePage);
  }

  @ApiOperation("根据案件ID 警报时间ID 设备ID 展示案件详情页")
  @GetMapping("/selectCaseDetailsById")
  public ResponseVo selectCaseDetailsById(Long wid, Long cid, Long did) {

    return caseManagementService.selectCaseDetailsById(wid, cid, did) == null
        ? ResponseVo.SERVER_ERROR
        : ResponseVo.successWithData(caseManagementService.selectCaseDetailsById(wid, cid, did));
  }

  @ApiOperation("根据案件ID 删除已经处理的案件")
  @DeleteMapping("/deleteCaseIsHanled/{cid}")
  public ResponseVo deleteCaseIsHanled(@PathVariable Long cid) {
    return caseManagementService.deleteCaseIsHanled(cid) <= 0
        ? ResponseVo.SERVER_ERROR
        : ResponseVo.SUCCESS;
  }

  @ApiOperation("根据案件ID 删除已经处理的案件 格式：'ids':[7,9,10]")
  @PostMapping("/deleteCaseIsHanledByIds")
  public ResponseVo deleteCaseIsHanledByIds(@RequestBody Map<String, List<Integer>> map) {
    List<Integer> idList = map.get("ids");
    return caseManagementService.deleteCaseIsHanledByIds(idList) <= 0
        ? ResponseVo.SERVER_ERROR
        : ResponseVo.SUCCESS;
  }

  @ApiOperation("生成告警事件图表")
  @RequestMapping(value = "/warnEventChart", method = RequestMethod.GET)
  public ResponseVo warnEventChart(
      @ApiParam(value = "开始时间 格式为 年-月-日", required = true)
          @NotBlank(message = "startTime 不能为null,格式为 年-月-日")
          String startTime,
      @ApiParam(value = "结束时间 格式为 年-月-日", required = true)
          @NotBlank(message = "endTime 不能为null,格式为 年-月-日")
          String endTime) {
    return warnEventService.warnEventChart(startTime, endTime);
  }

  @ApiOperation("案件追踪，需要传入当前案件的 cid")
  @GetMapping("/selectCaseTrack")
  public ResponseVo selectCaseTrack(Long id) {
    CaseTrackVo caseTrackVo = caseManagementService.selectCaseTrack(id);
    return caseTrackVo == null
        ? ResponseVo.NOT_FOUND_RESOURCES
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, caseTrackVo);
  }

  @ApiOperation("查询案件在已处理的数据 分页查询")
  @GetMapping("/selectCaseIsHandledPage")
  public ResponseVo selectCaseIsHandledPage(
      Long pageNo,
      Long pageSize,
      String dataOrigin,
      String deviceCode,
      String reason,
      String username,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为14
    pageNo = pageNo == null || pageNo < 1 ? 1L : pageNo;
    pageSize = pageSize == null || pageSize < 1 ? 14 : pageSize;
    Page<CaseIsHandledVo> page = new Page<>(pageNo, pageSize);
    Page<CaseIsHandledVo> voPage =
        caseManagementService.selectCaseIsHandledPage(
            page,
            caseType,
            warnType,
            dataOrigin,
            deviceCode,
            reason,
            username,
            startTimestamp,
            endTimestamp);
    List<CaseIsHandledVo> records = voPage.getRecords();
    for (CaseIsHandledVo record : records) {
      CaseIsHandledVo handledPageName =
          caseManagementService.selectCaseIsHandledPageName(record.getCid(), caseType);
      record.setName(handledPageName.getName());
    }
    return ResponseVo.successWithData(voPage);
  }
}
